package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.NursingElder;
import com.zzyl.nursing.dto.AlertDataHandleDto;
import com.zzyl.nursing.dto.AlterDataDto;
import com.zzyl.nursing.service.INursingElderService;
import com.zzyl.nursing.util.DateTimeZoneConverter;
import com.zzyl.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;

/**
 * 报警数据Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-05
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements IAlertDataService {
    @Autowired
    private AlertDataMapper alertDataMapper;
    @Autowired
    private ISysRoleService sysRoleService;

    /**
     * 查询报警数据
     *
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id) {
        return getById(id);
    }


    /**
     * 新增报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData) {
        return save(alertData) ? 1 : 0;
    }

    /**
     * 修改报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData) {
        return updateById(alertData) ? 1 : 0;
    }

    /**
     * 批量删除报警数据
     *
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除报警数据信息
     *
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 处理设备报警数据
     *
     * @param dto 报警数据
     * @return 结果
     */
    @Override

    public void handleAlertData(AlertDataHandleDto dto) {
        AlertData alertData = getById(dto.getId());
        System.out.println(dto.getProcessingResult());
        alertData.setProcessingResult(dto.getProcessingResult());
        //处理时间格式
        LocalDateTime time = LocalDateTimeUtil.parse(dto.getProcessingTime(),"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

        // 自定义格式化器，去掉 T，用空格或其他符号代替
        /*DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format = time.format(formatter);
        LocalDateTime time1 = LocalDateTimeUtil.parse(format);*/

        LocalDateTime localDateTime = DateTimeZoneConverter.utcToShanghai(time);
        System.out.println(localDateTime);
        alertData.setProcessingTime(localDateTime);

        alertData.setStatus(1);
        //拿操作人信息
        Long userId = alertData.getUserId();
        alertData.setProcessorName(sysRoleService.selectRoleNameById(userId));
        updateById(alertData);
    }

    /**
     * 查询报警数据列表
     *
     * @param dto 报警数据
     * @return 报警数据集合
     */
    @Override
    public TableDataInfo selectAlterDataList(AlterDataDto dto) {
        //护理员能看到对应的老人的报警数据
        Long userId = SecurityUtils.getUserId();

        Page<AlertData> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        LambdaQueryWrapper<AlertData> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(dto.getDeviceName()!=null,AlertData::getDeviceName, dto.getDeviceName())
                .between(dto.getStartTime()!=null,AlertData::getCreateTime, dto.getStartTime(), dto.getEndTime())
                .eq(dto.getStatus()!=null,AlertData::getStatus, dto.getStatus())
                .eq(AlertData::getUserId, userId);
        Page<AlertData> result = page(page, lambdaQueryWrapper);
        //构建返回值
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setRows(result.getRecords());
        tableDataInfo.setTotal(result.getTotal());
        tableDataInfo.setMsg("请求成功");
        tableDataInfo.setCode(200);
        return tableDataInfo;
    }
}
